package org.dromara.business.modules.favorites.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.business.modules.demands.mapper.BuDemandsMapper;
import org.dromara.business.modules.favorites.domain.BuFavorites;
import org.dromara.business.modules.favorites.domain.bo.BuFavoritesBo;
import org.dromara.business.modules.favorites.domain.vo.BuFavoritesVo;
import org.dromara.business.modules.favorites.domain.vo.MyCountVo;
import org.dromara.business.modules.favorites.mapper.BuFavoritesMapper;
import org.dromara.business.modules.favorites.service.IBuFavoritesService;
import org.dromara.business.modules.services.mapper.BuServicesMapper;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.domain.BaseEntity;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 用户收藏Service业务层处理
 *
 * @author ckk
 * @date 2025-11-12
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class BuFavoritesServiceImpl implements IBuFavoritesService {

    private final BuFavoritesMapper baseMapper;
    private final BuServicesMapper servicesMapper;
    private final BuDemandsMapper demandsMapper;

    /**
     * 分页查询用户收藏列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 用户收藏分页列表
     */
    @Override
    public TableDataInfo<BuFavoritesVo> queryPageList(BuFavoritesBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BuFavorites> lqw = buildQueryWrapper(bo);
        Page<BuFavoritesVo> result = baseMapper.queryPageList(pageQuery.build(), lqw);
        result.getRecords().forEach(v -> {
            if (v.getTargetType() == 1) {
                v.setServices(servicesMapper.selectVoById(v.getTargetId()));
            } else if (v.getTargetType() == 2) {
                v.setDemands(demandsMapper.selectVoById(v.getTargetId()));
            }
        });
        return TableDataInfo.build(result);
    }

    private LambdaQueryWrapper<BuFavorites> buildQueryWrapper(BuFavoritesBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BuFavorites> lqw = Wrappers.lambdaQuery();
        lqw.orderByDesc(BuFavorites::getCreateTime);
        lqw.eq(bo.getTargetId() != null, BuFavorites::getTargetId, bo.getTargetId());
        lqw.like(StringUtils.isNotBlank(bo.getTargetTitle()), BuFavorites::getTargetTitle, bo.getTargetTitle());
        lqw.eq(bo.getTargetType() != null, BuFavorites::getTargetType, bo.getTargetType());
        return lqw;
    }

    /**
     * 新增用户收藏
     *
     * @param bo 用户收藏
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(BuFavoritesBo bo) {
        BuFavorites add = MapstructUtils.convert(bo, BuFavorites.class);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 取消收藏
     */
    @Override
    public void cancel(Long targetId) {
        baseMapper.delete(Wrappers.lambdaQuery(BuFavorites.class)
            .eq(BuFavorites::getTargetId, targetId)
            .eq(BaseEntity::getCreateBy, LoginHelper.getUserId()));
    }

    @Override
    public MyCountVo myCount() {
        MyCountVo vo = new MyCountVo();
        Long userId = LoginHelper.getUserId();
        vo.setServerCount(new LambdaQueryChainWrapper<>(servicesMapper).eq(BaseEntity::getCreateBy, userId).count());
        vo.setDemandCount(new LambdaQueryChainWrapper<>(demandsMapper).eq(BaseEntity::getCreateBy, userId).count());
        vo.setFavoritesCount(new LambdaQueryChainWrapper<>(baseMapper).eq(BaseEntity::getCreateBy, userId).count());
        return vo;
    }

    @Override
    public Boolean isFavorites(Long id) {
        return new LambdaQueryChainWrapper<>(baseMapper)
            .eq(BuFavorites::getTargetId, id)
            .eq(BuFavorites::getCreateBy, LoginHelper.getUserId())
            .count() > 0;
    }
}
